import { FETCH_ERROR, FETCH_START, FETCH_SUCCESS, ClassifyAction } from "./classifyAction";

interface ClassifyState {
  error: null | string;
  loading: boolean;
  list: ListItemState[];
}

export interface ListItemState {
  title: string;
  id: string;
  children: {
    title: string;
    url: string;
    desc: string;
    id: string;
  }[];
}

const initState: ClassifyState = {
  list: [],
  error: null,
  loading: true,
};

export default (state = initState, action: ClassifyAction) => {
  switch (action.type) {
    case FETCH_START: {
      return {
        ...state,
        loading: true,
      };
    }
    case FETCH_SUCCESS: {
      return {
        ...state,
        loading: false,
        list: action.payload,
      };
    }
    case FETCH_ERROR: {
      return {
        ...state,
        loading: false,
        error: action.payload,
      };
    }
    default:
      return state;
  }
};
